/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proxy;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.TimeZone;

/**
 *
 * @author Nguyen Van Hue
 */
public class Restart extends Thread {
    private final long ONE_HOUR = 1000 * 60 * 60;
    
    @Override
    public void run() {
        try {
            TimeZone zone = TimeZone.getTimeZone("GMT+7");
            TimeZone.setDefault(zone);
            System.out.println(Calendar.getInstance(zone).getTime());
            Thread.sleep(3 * ONE_HOUR);
            while(true) {
                Thread.sleep(120000);
                TimeZone.setDefault(zone);
                Calendar cal = Calendar.getInstance(zone);
                if(cal.get(Calendar.HOUR_OF_DAY) == 5) {
                    int minute = cal.get(Calendar.MINUTE);
                    if(minute >= 30) {
                        break;
                    }
                }
            }
            exec("shutdown /r /t 0 /c \"E P\" ");
            System.exit(0);
        }
        catch(Exception ex) {
            System.out.println("Restart: " + ex);
        }
    }
    
    private int exec(String command) throws IOException {
        Process p = Runtime.getRuntime().exec(command);
        SystemOut(p.getInputStream());

        return p.exitValue();
    }
    
    private void SystemOut(InputStream in) throws IOException {
        int nByte;
        final byte[] buff = new byte[1024*1024];
        while((nByte = in.read(buff)) > 0) {
            System.out.write(buff,0, nByte);
        }
        in.close();
    }
}
